package com.stone.business.gzjl.Action;

import java.util.HashMap;
import java.util.List;

import com.stone.business.gzjl.pojo.TabGzjl;
import com.stone.business.gzrwd.pojo.TGzrwd;
import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.business.jc.service.JcCommonService;
import com.stone.business.plan.pojo.TBzgzrw;
import com.stone.para.IConstant;
import com.stone.sys.pojo.*;
import com.stone.utils.DateFormatTool;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;

@SuppressWarnings("serial")
public class GzjlAction extends BaseAction {

	private JcCommonService gzjlService;

	private TabGzjl entity;
	private TGzrwd gzrwd;
	private TBzgzrw bzgzrw;
	private List<TGzrwdRw> showList;
	private List<TGzrwdRw> bzgzrwList;
	private String flag;

	public String toDetail() {

		// 共作任务单主表
		gzrwd = (TGzrwd) gzjlService.findById(gzrwd.getId(), TGzrwd.class
				.getName());
		TabGzjl gzjl = (TabGzjl)gzjlService.findFirst(" from TabGzjl where gzrwdid = '"
				+ gzrwd.getId() + "'");
		if(gzjl!=null){
			entity=gzjl;
		}

		// 查询工作任务单任务列表
		showList = gzjlService.findListByHql("from TGzrwdRw where gzrwdid = '"
				+ gzrwd.getId() + "'");
		bzgzrwList=showList;
		List<TDictionary> listDic = gzjlService
				.findListByHql("from TDictionary where itemType = 'SBLX'");
		//by wangtao update20120515新建默认为系统当前用户日期
		if(entity.getId()==null){
			entity.setJlr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			// 记录日期
			this.entity.setJlrq(DateFormatTool.formatToDate(gzjlService.getSysdate("yyyy-MM-dd HH24:mi"),true));
		}
		for (TGzrwdRw rw : showList) {
			for (TDictionary dic : listDic) {
				if (dic.getItemId().equals(rw.getSblx())) {
					rw.setSblxname(dic.getItemName());
					break;
				}
			}
			if(StringUtils.isNullOrBlankFull(rw.getYlwt())){
			rw.setYlwt("无");//遗留问题初始化无by wangtao update20120514
			}
		}

		return "SUCCESS";
	}

	// 保存工作记录(工作记录回填)
	public String savegzjl() {
		gzjlService.savegzjl(entity, bzgzrwList);
		
		if ("1".equals(flag)) {
			return "EDIT-SUCCESS";
		} else {
			return null;
		}
	}
	// 归档工作记录(工作记录回填)
	public String savehistory() {
		gzjlService.savehistory(entity, bzgzrwList);
		
		if ("1".equals(flag)) {
			return "EDIT-SUCCESS";
		} else {
			return null;
		}
	}

	public String toDetailEdit() {
		String sql = "";
		if (!"".equals(entity.getId()) && entity.getId() != null) {
			entity = (TabGzjl) gzjlService.findById(entity.getId(), entity
					.getClass().getName());

			// 共作任务单株表
			gzrwd = (TGzrwd) gzjlService.findById(entity.getGzrwdid(), TGzrwd.class
					.getName());

			// 查询工作任务单任务列表
			showList = gzjlService
					.findListByHql("from TGzrwdRw where gzrwdid = '"
							+ entity.getGzrwdid() + "'");
			bzgzrwList=showList;
			List<TDictionary> listDic = gzjlService
					.findListByHql("from TDictionary where itemType = 'SBLX'");

			for (TGzrwdRw rw : showList) {
				for (TDictionary dic : listDic) {
					if (dic.getItemId().equals(rw.getSblx())) {
						rw.setSblxname(dic.getItemName());
						break;
					}
				}
			}
		}

		return "DETAILEDIT";
	}

	public JcCommonService getGzjlService() {
		return gzjlService;
	}

	public void setGzjlService(JcCommonService gzjlService) {
		this.gzjlService = gzjlService;
	}

	public TBzgzrw getBzgzrw() {
		return bzgzrw;
	}

	public void setBzgzrw(TBzgzrw bzgzrw) {
		this.bzgzrw = bzgzrw;
	}

	public List<?> getShowList() {
		return showList;
	}

	public void setShowList(List<TGzrwdRw> showList) {
		this.showList = showList;
	}

	public List<TGzrwdRw> getBzgzrwList() {
		return bzgzrwList;
	}

	public void setBzgzrwList(List<TGzrwdRw> bzgzrwList) {
		this.bzgzrwList = bzgzrwList;
	}

	public TabGzjl getEntity() {
		return entity;
	}

	public void setEntity(TabGzjl entity) {
		this.entity = entity;
	}

	public TGzrwd getGzrwd() {
		return gzrwd;
	}

	public void setGzrwd(TGzrwd gzrwd) {
		this.gzrwd = gzrwd;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

}
